<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>

<script type="text/javascript">
    //<![CDATA[
    function changeDiagramsPeriod(periodObj) {
        periodParam = periodObj.value ? 'period/' + periodObj.value + '/' : '';
<?php foreach ($this->getChild('diagrams')->getTabsIds() as $tabId): ?>
        ajaxBlockParam = 'block/tab_<?php echo $tabId ?>/';
        ajaxBlockUrl = '<?php echo $this->getUrl('*/*/ajaxBlock', array('_current' => true, 'block' => '', 'period' => '')) ?>' + ajaxBlockParam + periodParam;
        new Ajax.Request(ajaxBlockUrl, {
            parameters: {isAjax: 'true', form_key: FORM_KEY},
            onSuccess: function(transport) {
                tabContentElementId = '<?php echo $this->getChild('diagrams')->getId() ?>_<?php echo $tabId ?>_content';
                try {
                    if (transport.responseText.isJSON()) {
                        var response = transport.responseText.evalJSON()
                        if (response.error) {
                            alert(response.message);
                        }
                        if(response.ajaxExpired && response.ajaxRedirect) {
                            setLocation(response.ajaxRedirect);
                        }
                    } else {
                        $(tabContentElementId).update(transport.responseText);
                    }
                }
                catch (e) {
                    $(tabContentElementId).update(transport.responseText);
                }
            }
        });
<?php endforeach; ?>
        ajaxBlockUrl = '<?php echo $this->getUrl('*/*/ajaxBlock', array('_current' => true, 'block' => 'totals', 'period' => '')) ?>' + periodParam;
        new Ajax.Request(ajaxBlockUrl, {
            parameters: {isAjax: 'true', form_key: FORM_KEY},
            onSuccess: function(transport) {
                tabContentElementId = 'dashboard_diagram_totals';
                try {
                    if (transport.responseText.isJSON()) {
                        var response = transport.responseText.evalJSON()
                        if (response.error) {
                            alert(response.message);
                        }
                        if(response.ajaxExpired && response.ajaxRedirect) {
                            setLocation(response.ajaxRedirect);
                        }
                    } else {
                        $(tabContentElementId).replace(transport.responseText);
                    }
                }
                catch (e) {
                    $(tabContentElementId).replace(transport.responseText);
                }
            }
        });
    }
    function viewdetails(deal)
    {
        // deal = deal.value ? 'city/' + deal.value + '/' : '';
        ajaxBlockUrl = '<?php echo $this->getUrl('*/*/ajaxBlock', array('_current' => true, 'block' => 'summary_deal', 'city' => '')) ?>'+deal ;
        new Ajax.Request(ajaxBlockUrl, {
            parameters: {isAjax: 'true', form_key: FORM_KEY},
            onSuccess: function(transport) {
                tabContentElementId = 'dealSummaryGrid_table';
                try {
                    if (transport.responseText.isJSON()) {
                        var response = transport.responseText.evalJSON();
                        if (response.error) {
                            alert(response.message);
                        }
                        if(response.ajaxExpired && response.ajaxRedirect) {
                            setLocation(response.ajaxRedirect);
                        }
                    } else {
                        $(tabContentElementId).replace(transport.responseText);
                    }
                }
                catch (e) {
                    $(tabContentElementId).replace(transport.responseText);
                }
            }
        });
    }
    /* Calculating Number of deals in the dashboard Ajax */
    function changedeal(deal)
    {
        deal = deal.value ? 'city/' + deal.value + '/' : '';
        ajaxBlockUrl = '<?php echo $this->getUrl('*/*/ajaxBlock', array('_current' => true, 'block' => 'summary_deal', 'city' => '')) ?>'+deal ;
        new Ajax.Request(ajaxBlockUrl, {
            parameters: {isAjax: 'true', form_key: FORM_KEY},
            onSuccess: function(transport) {
                tabContentElementId = 'dealSummaryGrid_table';
                try {
                    if (transport.responseText.isJSON()) {
                        var response = transport.responseText.evalJSON();
                        if (response.error) {
                            alert(response.message);
                        }
                        if(response.ajaxExpired && response.ajaxRedirect) {
                            setLocation(response.ajaxRedirect);
                        }
                    } else {
                        $(tabContentElementId).replace(transport.responseText);
                    }
                }
                catch (e) {
                    $(tabContentElementId).replace(transport.responseText);
                }
            }
        });
    }
    function toggleCal(id) {
        $('dashboard_'+id+'_cal_div').toggle();
        $('dashboard_'+id+'_range_div').toggle();
    }
    //]]>
</script>
<?php
/* contus */
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', '548'); // get the cities attribute id 548
// to get the multiple cities in pages(drop down)
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option) {
    $value = $option['value'];
    if($value !='') {
        $cities[$value] = $option['label'];
        $select .= "<option value='".$option['value']."'>". $option['label']."</option>";
    }
}
$final=array_unique($cities);
?>
<div class="content-header">
    <table cellspacing="0">
        <tr>
            <td><h3 class="head-dashboard"><?php echo $this->__('Dashboard') ?></h3></td>
        </tr>
    </table>
</div>
<div class="dashboard-container">
    <?php echo $this->getChildHtml('store_switcher') ?>
    <table cellspacing="25" width="100%">
        <tr>
            <td><?php echo $this->getChildHtml('sales') ?>
                <div class="entry-edit">
                    <div class="entry-edit-head"><h4><?php echo $this->__('Deals Summary') ?></h4>
                        <select id="city" name="city"  class="select" style="float:right"  onchange="changedeal(this);">
                            <option value="0">Select City</option><?php echo $select; ?>
                        </select>
                    </div>
                    <fieldset class="np"><?php echo $this->getChildHtml('dealSummary'); ?></fieldset>
                </div>
                <div class="entry-edit">
                    <div class="entry-edit-head"><h4><?php echo $this->__('Last 5 Orders') ?></h4></div>
                    <fieldset class="np"><?php echo $this->getChildHtml('lastOrders'); ?></fieldset>
                </div>
                <!--<div class="entry-edit">
                    <div class="entry-edit-head"><h4><?php echo $this->__('Last 5 Search Terms') ?></h4></div>
                    <fieldset class="np"><?php echo $this->getChildHtml('lastSearches'); ?></fieldset>
                </div>-->
            </td>
            <td>
                <div class="entry-edit" style="border:1px solid #ccc;">
                    <?php echo $this->getChildHtml('diagrams') ?>
                    <div id="diagram_tab_content"></div>
                    <div style="margin:20px;">
                        <?php echo $this->getChildHtml('totals') ?>
                    </div>
                    <div style="margin:20px;">
                        <?php echo $this->getChildHtml('grids') ?>
                        <div id="grid_tab_content"></div>
                    </div>
                </div>
            </td>
        </tr>
    </table>
</div>
